import axios from 'axios'

/**
 * 处理文件的下载地址 group1/dfs/20250107145053687.png
 * 获取缓存的 wsAddress + url
 * replace wss to https
 * 文件的下载需要的wss协议，替换为https协议
 * @param url
 * @returns newUrl
 */
export const getFileDownLoadUrl = (url) => {
    let wsAddress = '';
    if (process.env.NODE_ENV === "development") {
        // 珺月环境
        wsAddress = 'https://192.168.1.100:8080'
        // wsAddress = 'https:/fzfntsjdmf.dfs.yelinksaas.com'
    } else {
        wsAddress = localStorage.getItem('wsAddress')
        // 替换wss为https
        wsAddress = wsAddress.replace('wss://', 'https://')
    }
    return wsAddress + '/' + url;
}

/**
 * 请求回来blob通过a链接下载文件
 * @param downLoadUrl
 * @param fileName
 */
export const downloadFile = async (downLoadUrl, fileName) => {
  let token;
  if (process.env.NODE_ENV === "development") {
    // 开发环境，使用硬编码的 token
    token = 'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJmQm9wV2otVTFIYjlTa0ZHMHZYQ1lRVVhkRkdDMGhXRTBkVjJCN1FaSl9ZIn0.eyJleHAiOjE3NDc2NDM2MDksImlhdCI6MTc0MjQ1OTYwOSwianRpIjoiYTczNDZlOTQtMDU2MS00NWRjLTk0M2EtNmM2MGY3ODgwZDVmIiwiaXNzIjoiaHR0cDovLzE3Mi4xNi4wLjM6ODQxMS9hdXRoL3JlYWxtcy9lZGdlIiwiYXVkIjpbIm5vY29jb25zb2xlIiwiYWNjb3VudCIsIm9jcy1jb250cm9sbGVyIl0sInN1YiI6ImIwMjZiYzUxLTEzMjItNDJiOC1iZTg0LWU5MDJiNmFjYmM3YSIsInR5cCI6IkJlYXJlciIsImF6cCI6Imppbmd6aGkiLCJzZXNzaW9uX3N0YXRlIjoiN2U1NGQzZWQtMjQ0Ny00Y2MzLWI2ZjktY2ZkYjE5MmYzMTUwIiwiYWNyIjoiMSIsInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyLnsr7liLYt57O757uf566h55CG5ZGYIiwi57O757uf6L-Q57u0Iiwib2ZmbGluZV9hY2Nlc3MiLCJ1bWFfYXV0aG9yaXphdGlvbiIsImRlZmF1bHQtcm9sZXMtZWRnZSJdfSwicmVzb3VyY2VfYWNjZXNzIjp7Im5vY29jb25zb2xlIjp7InJvbGVzIjpbIm5vY29fY29uc29sZV9hZG1pbl9yb2xlIl19LCJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX0sIm9jcy1jb250cm9sbGVyIjp7InJvbGVzIjpbIm9jcy1jb250cm9sbGVyLWFkbWluLXJvbGUiLCJvY3MtY29udHJvbGxlci1yZWFkLXJvbGUiXX19LCJzY29wZSI6InByb2ZpbGUgZW1haWwiLCJzaWQiOiI3ZTU0ZDNlZC0yNDQ3LTRjYzMtYjZmOS1jZmRiMTkyZjMxNTAiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsIm5hbWUiOiLmm77kuL3okI0iLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ6ZW5nbHAiLCJnaXZlbl9uYW1lIjoi5pu-5Li96JCNIiwiZW1haWwiOiJ6ZW5nbHBAdGVzdC5jb20ifQ.mSD_XViMpJsHRSHWKxoQHYOUTtvbZMAtEaXDQEVMbsEGT3nD0iYAcFCB4lVkcs7zCUTjPh1WQPO7of8Pk2VCfF9tlBTSB9HTzdQO9pbFvcO3sYyd3KpqRi_DdSLITKbS5W198bPTR60Vxa8an4CiSxW3MY3n2cVqiMJdJxEqvqtyld--qiKOGQajwqnirzcmA6mfI_VA6mvSwyfuMuVI1-frE7GVU4lfqimMITAADgo4uw6-BJcpmm_nQsRqNBTwGuYyQsZGcpOKSZ_Y9JFNX-RrQLfaiPbXrNbWN-e6S8Zsg19_fsnShV_6OKZKgyJldYaX-15H1Lu7Bk-MDh4cEQ';
  } else {
    // 生产环境，从本地存储中获取 token
    token = localStorage.getItem('token');
  }

  try {
        const response = await axios.get(downLoadUrl, {
            responseType: 'blob', // 重要：指定返回数据的类型为二进制流
            // headers: {
            //                 Authorization: `Bearer ${token}` // 设置 Authorization 字段
            //              }
        });
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', `${fileName}`); // 指定下载后文件的名称
        document.body.appendChild(link);
        link.click(); // 触发下载
        document.body.removeChild(link); // 清理DOM
    } catch (error) {
        console.log(error)
    }
}